package dt.inlee.common.pagination;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;


/**
 * Criteria�����ѯ������
 *
 */
public abstract class CriteriaUtil
{
	/**
	 * ������������
	 * 
	 * @param Criteria�����ѯ����
	 * @param ��������
	 */
	public static void processParams(Criteria criteria, List<ConditionBean> conditions)
	{
		if (conditions == null)
		{
			return;
		}
		for (ConditionBean obj : conditions)
		{ 
			processParam(criteria, obj);
		}
	}

	/**
	 * ���?���������
	 * 
	 * @param Criteria�����ѯ����
	 * @param �����������
	 */
	private static void processParam(Criteria criteria, ConditionBean condition)
	{
		if(condition == null)
		{
			return;
		}
		ConditionType type = condition.getType();
		switch (type)
		{
			case LIKE:
			{
				criteria.add(Restrictions.like(condition.getField(), condition.getValues()[0]));
				break;
			}
			case BETWEEN:
			{
				criteria.add(Restrictions.between(condition.getField(), condition.getValues()[0], condition.getValues()[1]));
				break;
			}
			case EQUAL:
			{
				criteria.add(Restrictions.eq(condition.getField(), condition.getValues()[0]));
				break;
			}
			case GREATTHAN:
			{
				criteria.add(Restrictions.gt(condition.getField(), condition.getValues()[0]));
				break;
			}
			case LESSTHAN:
			{
				criteria.add(Restrictions.lt(condition.getField(), condition.getValues()[0]));
				break;
			}
			case IN:
			{
				criteria.add(Restrictions.in(condition.getField(), condition.getValues()));
				break;
			}
			case ISNULL:
			{
				criteria.add(Restrictions.isNull(condition.getField()));
				break;
			}
			case NOTEQUEAL:
			{
				criteria.add(Restrictions.ne(condition.getField(), condition.getValues()[0]));					
				break;
			}
			case GREATEQUALTHAN://���ڵ���
			{
				criteria.add(Restrictions.ge(condition.getField(), condition.getValues()[0]));					
				break;
			}
			case LESSEQUALTHAN://С�ڵ���
			{
				criteria.add(Restrictions.le(condition.getField(), condition.getValues()[0]));					
				break;
			}
			case NOTIN: //����ĳ��Χ
			{		
				criteria.add(Restrictions.not(Restrictions.in(condition.getField(), condition.getValues())));
				break;
			}
			case OR: //����AND
			{		
				ProcessExpress(criteria, condition);
				//criteria.add(Restrictions.or(Restrictions.eq(condition.getField(), condition.getValues()[0]), Restrictions.eq(condition.getField(), condition.getValues()[1])));

				break;
			}
			case ORLIKE: //����LIKE
			{		
				criteria.add(Restrictions.or(Restrictions.like(condition.getField(), condition.getValues()[0]), Restrictions.like(condition.getField1(), condition.getValues()[1])));

				break;
			}
			case ORBYVALUE: //����
			{		
				criteria.add(Restrictions.or((Criterion )condition.getValues()[0],(Criterion )condition.getValues()[1]));
				break;
			}
			case ISNOT:
			{
				criteria.add(Restrictions.isNotNull(condition.getField()));
				break;
			}
			default:
			{
				break;
			}
		}
	}

	private static void ProcessExpress(Criteria criteria, ConditionBean condition)
	{
		List<Criterion> criterions = new ArrayList<Criterion>();
        List<Criterion> criterionList = condition.getCriterionList();

        for (int i = 0; i < criterionList.size(); i++)
        {
            criterions.add(criterionList.get(i));
        }

        for (int i = criterionList.size(); i < 45; i++)
        {
            Criterion s = Restrictions.eq("id", -1);

            criterions.add(s);
        }

        //condition.CriterionList.Clear();
        criteria.add(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.or(criterions.get(44), criterions.get(43)), criterions.get(42)), criterions.get(41)), criterions.get(40)), criterions.get(39)), criterions.get(38)), criterions.get(37)), criterions.get(36)), criterions.get(35)), criterions.get(34)), criterions.get(33)), criterions.get(32)), criterions.get(31)), criterions.get(30)), criterions.get(29)), criterions.get(28)), criterions.get(27)), criterions.get(26)), criterions.get(25)), criterions.get(24)), criterions.get(23)), criterions.get(22)), criterions.get(21)), criterions.get(20)), criterions.get(19)), criterions.get(18)), criterions.get(17)), criterions.get(16)), criterions.get(15)), criterions.get(14)), criterions.get(13)), criterions.get(12)), criterions.get(11)), criterions.get(10)), criterions.get(9)), criterions.get(8)), criterions.get(7)), criterions.get(6)), criterions.get(5)), criterions.get(4)), criterions.get(3)), criterions.get(2)), criterions.get(1)), criterions.get(0)));
    }
	/**
	 * ��������ʵ�弯��
	 * 
	 * @param Criteria�����ѯ����
	 * @param ����ʵ�弯��
	 */
	public static void processOrders(Criteria criteria, List<OrderBean> orders)
	{
		if (orders == null)
		{
			return;
		}
		for (OrderBean order : orders)
		{
			processOrder(criteria, order);
		}
	}

	/**
	 * ���?������ʵ��
	 * 
	 * @param Criteria�����ѯ����
	 * @param ����ʵ��
	 */
	private static void processOrder(Criteria criteria, OrderBean order)
	{
		if(order == null)
		{
			return;
		}
		if (order.isAsc())
		{
			criteria.addOrder(Order.asc(order.getField()));
		}
		else
		{
			criteria.addOrder(Order.desc(order.getField()));
		}
	}
}
